/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.coffee.monitor.web.device;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.coffee.monitor.entity.Device;
import com.coffee.monitor.entity.DeviceGroup;
import com.coffee.monitor.entity.Sensor;
import com.coffee.monitor.service.account.ShiroDbRealm.ShiroUser;
import com.coffee.monitor.service.device.DeviceGroupService;
import com.coffee.monitor.service.device.DeviceService;
import com.coffee.monitor.service.device.SensorService;

/**
 * LoginController负责打开登录页面(GET请求)和登录出错页面(POST请求)，
 * 
 * 真正登录的POST请求由Filter完成,
 * 
 * @author coffee
 */
@Controller
@RequestMapping(value = "/user")
public class UserDeiveController {

	@Autowired
	private DeviceService deviceService;
	@Autowired
	private DeviceGroupService groupService;
	@Autowired
	private SensorService sensorService;
	
	
	
	@RequestMapping(value = "/groups",method = RequestMethod.GET)
	public String groups(Model model) {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		List<DeviceGroup> groups=groupService.findByUserId(user.getId());
		model.addAttribute("groups", groups);
		return "account/login";
	}
	@RequestMapping(value = "/addGroup",method = RequestMethod.POST)
	public String addGroups(DeviceGroup group,Model model) {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		group.setUserId(user.getId());
		groupService.save(group);
		return "redirect:/user/groups";
	}
	
	@RequestMapping(value = "/devices/{gid}",method = RequestMethod.GET)
	public String devices(@PathVariable(value="gid") Long gid,Model model) {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		List<Device> devices=deviceService.findByDeviceGroupAndUserId(gid,user.getId());
		model.addAttribute("devices", devices);
		return "account/login";
	}
	@RequestMapping(value = "/addDevice/{gid}",method = RequestMethod.POST)
	public String addDevice(@PathVariable(value="gid") Long gid,Model model,Device device) {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		DeviceGroup group=groupService.find(gid);
		if (group!=null) {
			device.setUserId(user.getId());
			device.setGroup(group);
			deviceService.save(device);
		}
		return "redirect:/user/devices/"+gid;
	}
	
	@RequestMapping(value = "/sensors/{did}",method = RequestMethod.GET)
	public String sensors(@PathVariable(value="did") Long did,Model model) {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		List<Sensor> sensors=sensorService.findByDeviceAndUserId(did,user.getId());
		model.addAttribute("sensors", sensors);
		return "account/login";
	}
	@RequestMapping(value = "/addSensor/{did}",method = RequestMethod.POST)
	public String addDevice(@PathVariable(value="did") Long did,Model model,Sensor sensor) {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		Device device=deviceService.find(did);
		if (device!=null) {
			sensor.setUserId(user.getId());
			sensor.setDevice(device);
			sensorService.save(sensor);
		}
		return "redirect:/user/sensors/"+did;
	}
}
